package com.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pet.mapper.SysUserMapper;
import com.pet.service.SysMenuService;
import com.pet.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.system.SysUser;
import com.pet.utils.CustomUser;
import com.pet.vo.RouterVo;
import com.pet.vo.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yph
 * @since 2023-07-29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String,Object> map=new HashMap<>();
        SysUser sysUser=this.getUserInfoUserName(username);
        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList = sysMenuService.findUserMenuList(sysUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList=sysMenuService.findUserpermsList(sysUser.getId());

        //当前权限控制使用不到，暂时忽略
        map.put("name",sysUser.getName());
        map.put("avatar",sysUser.getHeadUrl());
        map.put("roles",  "[admin]");

        map.put("buttons",permsList);
        map.put("routers",routerVoList);
        System.out.println("");
        System.out.println("============map的内容是:==============="+map.toString());
        System.out.println("");
        return map;
    }

    @Override
    public SysUser getUserInfoUserName(String username) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        SysUser sysUser=this.baseMapper.selectOne(queryWrapper);
        return sysUser;

    }
    @Override
    public IPage<SysUser> selectpage(IPage<SysUser> iPage, SysUserQueryVo sysUserQueryVo) {

        return this.baseMapper.selectPage(iPage,sysUserQueryVo);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //1.查询当前用户
        SysUser sysUser=this.baseMapper.selectById(id);
        //2.设置状态
        sysUser.setStatus(status);
        //3.更新
        this.baseMapper.updateById(sysUser);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        SysUser sysUser = getUserInfoUserName(username);
//        if(sysUser==null){
//            throw new UsernameNotFoundException("账号不存在");
//        }else if(sysUser.getStatus()==0){
//            throw new RuntimeException("账号已停用");
//        }else {
//            CustomUser customUser=new CustomUser();
//            customUser.setUsername(sysUser.getUsername());
//            customUser.setPassword(sysUser.getPassword());
//            customUser.setStatus(sysUser.getStatus());
//            return customUser;
//        }
        SysUser sysUser = getUserInfoUserName(username);
        if (null == sysUser) {
            throw new UsernameNotFoundException("用户名不存在！");
        }

        if (sysUser.getStatus().intValue() == 0) {
            throw new RuntimeException("账号已停用");

        } else {
            CustomUser customUser = new CustomUser();
            customUser.setUsername(sysUser.getUsername());
            customUser.setPassword(sysUser.getPassword());
            customUser.setId(sysUser.getId());

            List<String> userPermsList = sysMenuService.findUserpermsList(sysUser.getId());
            List<GrantedAuthority> authorities = new ArrayList<>();
            for (String perm : userPermsList) {
                authorities.add(new SimpleGrantedAuthority(perm.trim()));
            }
            customUser.setAuthorities(authorities);
            return customUser;
        }
    }
}
